package user

import (
	"fmt"
	"regexp"
)

type Mobile struct {
	mobile      string
	validated   bool
	countryCode string
}

var countryCodeValidMap = map[string]func(string) bool{
	"86": cnMobileValid, // cn
}

func NewMobile(mobile string) *Mobile {
	m := &Mobile{
		mobile: mobile,
	}
	return m
}

func (m *Mobile) SetCountryCode(countryCode string) {
	m.countryCode = countryCode
	m.validate()
}

func (m *Mobile) GetMobile() string {
	return m.mobile
}

func (m *Mobile) IsValidated() bool {
	if m.countryCode == "" {
		m.validated = cnMobileValid(m.mobile)
	}
	return m.validated
}

func (m *Mobile) validate() {
	validateFunc, ok := countryCodeValidMap[m.countryCode]
	if ok {
		m.validated = validateFunc(m.mobile)
	} else {
		m.validated = true
	}
}

func cnMobileValid(code string) bool {
	regex := `^1[3-9]\d{9}$`
	match, _ := regexp.MatchString(regex, code)
	fmt.Println("xxxxx", match)
	return match
}
